package timing.ukulele.storage.service;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Example;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import timing.ukulele.common.data.ResponseCode;
import timing.ukulele.common.data.ResponseData;
import timing.ukulele.storage.entity.MetadataInfoEntity;
import timing.ukulele.storage.model.MetadataInfoModel;
import timing.ukulele.storage.model.MetadataInfoSaveModel;
import timing.ukulele.storage.model.MetadataInfoUpdateModel;
import timing.ukulele.storage.repository.MetadataInfoRepository;
import timing.ukulele.storage.util.UpdateUtil;
import timing.ukulele.storage.vo.file.FileMetadataInfoVO;

import java.util.ArrayList;
import java.util.List;

/**
 * 文件元数据接口实现类
 */
@Slf4j
@Service
public class MetadataService {

    private final MetadataInfoRepository repository;
    private final MongoOperations mongoOperations;

    public MetadataService(MetadataInfoRepository repository, MongoOperations mongoOperations) {
        this.repository = repository;
        this.mongoOperations = mongoOperations;
    }

    /**
     * 根据条件列表查询
     *
     * @param searchDTO 查询条件
     * @return 列表结果集
     */
    public List<FileMetadataInfoVO> list(MetadataInfoModel searchDTO) {

        // 组装查询参数
        Example<MetadataInfoEntity> queryWrapper = buildParams(searchDTO);

        List<MetadataInfoEntity> fileMetadataInfoEntityList = repository.findAll(queryWrapper);

        List<FileMetadataInfoVO> fileMetadataInfoVoList = new ArrayList<>();

        for (MetadataInfoEntity fileMetadataInfoEntity : fileMetadataInfoEntityList) {
            FileMetadataInfoVO fileMetadataInfoVo = new FileMetadataInfoVO();
            BeanUtils.copyProperties(fileMetadataInfoEntity, fileMetadataInfoVo);
            fileMetadataInfoVoList.add(fileMetadataInfoVo);
        }

        return fileMetadataInfoVoList;
    }

    /**
     * 根据条件单条查询
     *
     * @param searchDTO 查询条件
     * @return 单条结果
     */
    public FileMetadataInfoVO one(MetadataInfoModel searchDTO) {

        // 组装查询参数
        Example<MetadataInfoEntity> queryWrapper = buildParams(searchDTO);

        MetadataInfoEntity fileMetadataInfoEntity = repository.findOne(queryWrapper).orElse(null);

        FileMetadataInfoVO fileMetadataInfoVo = new FileMetadataInfoVO();

        if (null != fileMetadataInfoEntity) {
            BeanUtils.copyProperties(fileMetadataInfoEntity, fileMetadataInfoVo);
        }

        return fileMetadataInfoVo;
    }

    /**
     * 新增
     *
     * @param saveDTO 数据实体
     * @return 执行结果
     */
    public FileMetadataInfoVO save(MetadataInfoSaveModel saveDTO) {

        MetadataInfoEntity fileMetadataInfoEntity = new MetadataInfoEntity();
        BeanUtils.copyProperties(saveDTO, fileMetadataInfoEntity);
        MetadataInfoEntity result = repository.save(fileMetadataInfoEntity);

        FileMetadataInfoVO fileMetadataInfoVo = new FileMetadataInfoVO();
        BeanUtils.copyProperties(result, fileMetadataInfoVo);
        return fileMetadataInfoVo;
    }

    /**
     * 修改数据
     *
     * @param updateDTO 数据实体
     * @return 执行结果
     */
    public ResponseData<FileMetadataInfoVO> update(MetadataInfoUpdateModel updateDTO) {
        MetadataInfoEntity fileMetadataInfoEntity = new MetadataInfoEntity();
        BeanUtils.copyProperties(updateDTO, fileMetadataInfoEntity);
        try {
            mongoOperations.upsert(new Query(Criteria.where("_id").is(fileMetadataInfoEntity.getId())), UpdateUtil.getUpdateValues(fileMetadataInfoEntity), fileMetadataInfoEntity.getClass());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            return ResponseData.error(ResponseCode.BUSINESS_ERROR.getCode(), "修改失败");
        }

        FileMetadataInfoVO fileMetadataInfoVo = new FileMetadataInfoVO();
        BeanUtils.copyProperties(fileMetadataInfoEntity, fileMetadataInfoVo);

        return ResponseData.success(fileMetadataInfoVo);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 执行结果
     */
    public Boolean remove(String id) {
        repository.deleteById(id);
        return true;
    }

    private Example<MetadataInfoEntity> buildParams(MetadataInfoModel searchDTO) {
        // 组装查询参数

        MetadataInfoEntity filter = new MetadataInfoEntity();
        if (StringUtils.hasText(searchDTO.getFileKey())) {
            filter.setFileKey(searchDTO.getFileKey());
        }
        if (StringUtils.hasText(searchDTO.getFileMd5())) {
            filter.setFileMd5(searchDTO.getFileMd5());
        }
        if (StringUtils.hasText(searchDTO.getBucket())) {
            filter.setStorageBucket(searchDTO.getBucket());
        }
        if (StringUtils.hasText(searchDTO.getCreateBy())) {
            filter.setCreateBy(filter.getCreateBy());
        }
        return Example.of(filter);
    }
}
